package com.wsoft.form.util;

import cn.hutool.core.collection.CollUtil;
import com.wsoft.constant.FieldConvertConstant;
import com.wsoft.form.entity.WfFormColumnEntity;
import com.wsoft.form.enums.FieldKindEnum;
import kotlin.Triple;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author z001
 * @Date 2024/11/14 08:47
 */
public class FormUtil {


    /**
     * 自定义表单 字段转换
     * 包含字典 和 自动的字段
     * @param convertDataMap 转换map
     * @param columnList 字段列表
     * @param columnName 带转换字段
     * @param value 原本的值
     * @return
     */
    public static Object convertData(Map<Triple<String, String, String>, Object> convertDataMap,
                                     List<WfFormColumnEntity> columnList,
                                     String columnName,
                                     Object value) {
        List<WfFormColumnEntity> collect = columnList.stream().filter(item -> item.getColumnName().equals(columnName)).collect(Collectors.toList());
        if (CollUtil.isEmpty(collect)) {
            return value;
        }
        WfFormColumnEntity columnEntity = collect.get(0);
        if (null == columnEntity.getFieldKind()) {
            return value;
        }
        String code = null;
        String second = "";
        if (FieldKindEnum.SELECT_USER.getFieldKind().equals(columnEntity.getFieldKind())) {
            //人员
            code = FieldConvertConstant.MEMBER;
        } else if (FieldKindEnum.SELECT_ORGANIZATION.getFieldKind().equals(columnEntity.getFieldKind())) {
            //组织
            code = FieldConvertConstant.ORGANIZATION;
        } else if (FieldKindEnum.SELECT_POST.getFieldKind().equals(columnEntity.getFieldKind())) {
            //组织
            code = FieldConvertConstant.POST;
        } else if (FieldKindEnum.SELECT_LEVEL.getFieldKind().equals(columnEntity.getFieldKind())) {
            //组织
            code = FieldConvertConstant.LEVEL;
        } else if (FieldKindEnum.DICT.getFieldKind().equals(columnEntity.getFieldKind())) {
            //组织
            code = FieldConvertConstant.DICT;
            second = columnEntity.getDictCode();
        }
        value = convertDataMap.get(new Triple<>(code, second, value.toString())) == null ? value : convertDataMap.get(new Triple<>(code, second, value.toString())).toString();
        return value;
    }
}
